<!--
  * 类型管理
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="4">
          <a-form-item label="类型名称" class="smart-margin-bottom0">
            <a-input v-model:value.trim="queryForm.level_name" placeholder="名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <!-- <a-button @click="add()" type="primary" v-privilege="'level:add'">
          <template #icon>
            <PlusOutlined />
          </template>
          新建
        </a-button>

        <a-button @click="deleteData(selectedRowKeyList)" danger :disabled="selectedRowKeyList.length === 0" v-privilege="'level:del'">
          <template #icon>
            <DeleteOutlined />
          </template>
          批量删除
        </a-button> -->
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'level_name'">
            <a @click="detail(record)">{{ text }}</a>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button @click="detail(record)" type="link" style="color: #97999c">详情</a-button>
              <!-- <a-button @click="add(record)" type="link" v-privilege="'level:edit'">编辑</a-button>
              <a-button @click="deleteData(record.id, record.level_name)" danger type="link" v-privilege="'level:del'">删除</a-button> -->
            </div>
          </template>
        </template>
      </a-table>
    </div>
    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
    </div>

    <LevelFormModal ref="formModal" @reloadList="queryData" />
    <!---------- 详情 begin ----------->
    <LevelDetailsModal ref="detailRef" />
    <!---------- 详情 end ----------->
  </a-card>
</template>
<script setup lang="ts">
import _ from 'lodash'
import LevelDetailsModal from './components/level-details-modal.vue'
import LevelFormModal from './components/level-form-modal.vue'
import { message } from 'ant-design-vue'
import { errorSentry } from '@/lib/error-sentry'
import { levelApi } from '@/api/business/data/level-api'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import TableOperator from '@/components/support/table-operator/index.vue'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useQuery } from '@/hooks/web/use-query-hook'
import { QUERY_FORM_OP } from '@/constants/support/query-form-op-const'
import { useAppConfigStore } from '@/store/modules/app-store'
import { useResize } from '@/hooks/web/use-resize-hook'
const appConfigStore = useAppConfigStore()

// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = computed(() => [
  {
    title: '类型名称',
    dataIndex: 'level_name'
  },
  {
    title: '备注',
    dataIndex: 'remark'
  },
  {
    title: '创建时间',
    dataIndex: 'create_time'
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: appConfigStore.$state.compactFlag ? 120 : 130
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData, deleteData } = useQuery({
  fetchDataApi: async (params) => {
    const res = await levelApi.queryLevelList(params)
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  fetchDeleteApi: async (id) => {
    const res = await levelApi.delLevel({ id })
    return !!res
  },
  queryFormFilter: {
    level_name: ''
  },
  queryFormOp: {
    level_name: QUERY_FORM_OP['模糊查询']
  }
})
onMounted(queryData)

// 选择表格行
const selectedRowKeyList = ref<number[]>([])

function onSelectChange(selectedRowKeys: number[]) {
  selectedRowKeyList.value = selectedRowKeys
}

// ---------------------------- 添加/修改 ----------------------------
const formModal = ref()

function add(goodsData) {
  formModal.value.showDrawer(goodsData)
}
// ---------------------------- 详情 ----------------------------
const detailRef = ref()

const detail = (goodsData) => {
  detailRef.value.showDrawer(goodsData)
}
// ---------------------------- 修改状态 ----------------------------
const checkLoading = ref(false)
const modifyAction = async (checked, record) => {
  try {
    checkLoading.value = true
    const res = await driverApi.modifyDriver({
      id: record.id,
      field: 'status',
      value: checked == true ? 1 : 2
    })
    message.success(res.msg)
    queryData()
  } catch (e) {
    message.error(e.msg)
    errorSentry.captureError(e)
  } finally {
    checkLoading.value = false
  }
}
</script>
